﻿using System.Collections;
using System.Xml;
using InfoService.Utils;
using MediaPortal.Configuration;
using MediaPortal.Profile;

namespace InfoService
{
    public class InfoServiceSkinSettings
    {
        #region Object Instance
        private static InfoServiceSkinSettings instance = null;
        public static InfoServiceSkinSettings InfoServiceSkinSettingsInstance
        {
            get { return instance ?? (instance = new InfoServiceSkinSettings()); }
            set
            {
            }
        }
        #endregion

        #region Private variables
        private static readonly Logger logger = Logger.GetInstance();
        private IDictionary defines = null;
        private Settings settings = null;
        #endregion

        #region Feeds
        private string feeds_Enabled = string.Empty;
        protected string Feeds_Enabled
        {
            get
            {
                return feeds_Enabled;
            }
            set
            {
                feeds_Enabled = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Feeds.Enabled with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    if (value.ToUpper() == "TRUE" || value.ToUpper() == "YES")
                    {
                        settings.SetValueAsBool("InfoService", "FeedTickerEnabled", true);
                        feeds_Enabled = "TRUE";
                    }
                    else if (value.ToUpper() == "FALSE" || value.ToUpper() == "NO")
                    {
                        settings.SetValueAsBool("InfoService", "FeedTickerEnabled", false);
                        feeds_Enabled = "FALSE";
                    }
                }
            }
        }

        private string feeds_General_RefreshFeedEvery;
        protected string Feeds_General_RefreshFeedEvery
        {
            get
            {
                return feeds_General_RefreshFeedEvery;
            }
            set
            {
                feeds_General_RefreshFeedEvery = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Feeds.General.RefreshFeedEvery with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    int valueInt;
                    if (int.TryParse(value, out valueInt))
                    {
                        settings.SetValue("InfoService", "FeedRefreshInterval", valueInt);
                        feeds_General_RefreshFeedEvery = valueInt.ToString();
                    }
                }
            }
        }

        private string feeds_Layout_ShowFeedItemPublishTime;
        protected string Feeds_Layout_ShowFeedItemPublishTime
        {
            get
            {
                return feeds_Layout_ShowFeedItemPublishTime;
            }
            set
            {
                feeds_Layout_ShowFeedItemPublishTime = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Feeds.Layout.ShowFeedItemPublishTime with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    if (value.ToUpper() == "TRUE" || value.ToUpper() == "YES")
                    {
                        settings.SetValueAsBool("InfoService", "FeedShowItemPublishTime", true);
                        feeds_Layout_ShowFeedItemPublishTime = "TRUE";
                    }
                    else if (value.ToUpper() == "FALSE" || value.ToUpper() == "NO")
                    {
                        settings.SetValueAsBool("InfoService", "FeedShowItemPublishTime", false);
                        feeds_Layout_ShowFeedItemPublishTime = "FALSE";
                    }
                }
            }
        }

        private string feeds_Layout_MaxItemsPerFeedForAllFeeds;
        protected string Feeds_Layout_MaxItemsPerFeedForAllFeeds
        {
            get
            {
                return feeds_Layout_MaxItemsPerFeedForAllFeeds;
            }
            set
            {
                feeds_Layout_MaxItemsPerFeedForAllFeeds = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Feeds.Layout.MaxItemsPerFeedForAllFeeds with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    int valueInt;
                    if (int.TryParse(value, out valueInt))
                    {
                        settings.SetValue("InfoService", "FeedItemsCountPerFeed", valueInt);
                        feeds_Layout_MaxItemsPerFeedForAllFeeds = valueInt.ToString();
                    }
                }
            }
        }

        private string feeds_Layout_MaxItemsForFeedTicker;
        protected string Feeds_Layout_MaxItemsForFeedTicker
        {
            get
            {
                return feeds_Layout_MaxItemsForFeedTicker;
            }
            set
            {
                feeds_Layout_MaxItemsForFeedTicker = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Feeds.Layout.MaxItemsForFeedTicker with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    int valueInt;
                    if (int.TryParse(value, out valueInt))
                    {
                        settings.SetValue("InfoService", "FeedItemsCount", valueInt);
                        feeds_Layout_MaxItemsForFeedTicker = valueInt.ToString();
                    }
                }
            }
        }

        private string feeds_TickerLayout_TickerMask;
        protected string Feeds_TickerLayout_TickerMask
        {
            get
            {
                return feeds_TickerLayout_TickerMask; 
            }
            set
            {
                feeds_TickerLayout_TickerMask = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Feeds.TickerLayout.TickerMask with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    settings.SetValue("InfoService", "FeedTickerMask", value);
                    feeds_TickerLayout_TickerMask = value;
                }
            }
        }

        private string feeds_TickerLayout_Separator;
        protected string Feeds_TickerLayout_Separator
        {
            get
            {
                return feeds_TickerLayout_Separator;
            }
            set
            {
                feeds_TickerLayout_Separator = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Feeds.TickerLayout.Separator with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    settings.SetValue("InfoService", "FeedSeparator", value);
                    feeds_TickerLayout_Separator = value;
                }
            }
        }
        #endregion

        #region Weather
        private string weather_Enabled;
        protected string Weather_Enabled
        {
            get
            {
                return weather_Enabled;
            }
            set
            {
                weather_Enabled = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Weather.Enabled with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    if (value.ToUpper() == "TRUE" || value.ToUpper() == "YES")
                    {
                        settings.SetValueAsBool("InfoService", "WeatherEnabled", true);
                        weather_Enabled = "TRUE";
                    }
                    else if (value.ToUpper() == "FALSE" || value.ToUpper() == "NO")
                    {
                        settings.SetValueAsBool("InfoService", "WeatherEnabled", false);
                        weather_Enabled = "FALSE";
                    }
                }
            }
        }

        private string weather_General_RefreshWeatherEvery;
        protected string Weather_General_RefreshWeatherEvery
        {
            get
            {
                return weather_General_RefreshWeatherEvery;
            }
            set
            {
                weather_General_RefreshWeatherEvery = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Weather.General.RefreshWeatherEvery with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    int valueInt;
                    if (int.TryParse(value, out valueInt))
                    {
                        settings.SetValue("InfoService", "WeatherRefreshInterval", valueInt);
                        weather_General_RefreshWeatherEvery = valueInt.ToString();
                    }
                }
            }
        }
        #endregion

        #region Twitter
        private string twitter_Enabled;
        protected string Twitter_Enabled
        {
            get
            {
                return twitter_Enabled;
            }
            set
            {
                twitter_Enabled = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Twitter.Enabled with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    if (value.ToUpper() == "TRUE" || value.ToUpper() == "YES")
                    {
                        settings.SetValueAsBool("InfoService", "TwitterTickerEnabled", true);
                        twitter_Enabled = "TRUE";
                    }
                    else if (value.ToUpper() == "FALSE" || value.ToUpper() == "NO")
                    {
                        settings.SetValueAsBool("InfoService", "TwitterTickerEnabled", false);
                        twitter_Enabled = "FALSE";
                    }
                }
            }
        }

        protected string twitter_General_RefreshTwitterEvery;
        protected string Twitter_General_RefreshTwitterEvery
        {
            get
            {
                return twitter_General_RefreshTwitterEvery;
            }
            set
            {
                twitter_General_RefreshTwitterEvery = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Twitter.General.RefreshTwitterEvery with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    int valueInt;
                    if (int.TryParse(value, out valueInt))
                    {
                        settings.SetValue("InfoService", "TwitterRefreshInterval", valueInt);
                        twitter_General_RefreshTwitterEvery = valueInt.ToString();
                    }
                }
            }
        }

        protected string twitter_Layout_MaxItemsForTwitterTicker;
        protected string Twitter_Layout_MaxItemsForTwitterTicker
        {
            get
            {
                return twitter_Layout_MaxItemsForTwitterTicker;
            }
            set
            {
                twitter_Layout_MaxItemsForTwitterTicker = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Twitter.Layout.MaxItemsForTwitterTicker with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    int valueInt;
                    if (int.TryParse(value, out valueInt))
                    {
                        settings.SetValue("InfoService", "TwitterItemsCount", valueInt);
                        twitter_Layout_MaxItemsForTwitterTicker = valueInt.ToString();
                    }
                }
            }
        }

        protected string twitter_TickerLayout_TickerMask;
        protected string Twitter_TickerLayout_TickerMask
        {
            get
            {
                return twitter_TickerLayout_TickerMask;
            }
            set
            {
                twitter_TickerLayout_TickerMask = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Twitter.TickerLayout.TickerMask with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    settings.SetValue("InfoService", "TwitterTickerMask", value);
                    twitter_TickerLayout_TickerMask = value;
                }
            }
        }

        protected string twitter_TickerLayout_Separator;
        protected string Twitter_TickerLayout_Separator
        {
            get
            {
                return twitter_TickerLayout_Separator;
            }
            set
            {
                twitter_TickerLayout_Separator = string.Empty;
                if (!string.IsNullOrEmpty(value))
                {
                    logger.WriteLog(string.Format("Loading skin setting #InfoService.SkinSettings.Twitter.TickerLayout.Separator with value '{0}'", value), LogLevel.Debug, InfoServiceModul.SkinSettings);

                    settings.SetValue("InfoService", "TwitterSeparator", value);
                    twitter_TickerLayout_Separator = value;
                }
            }
        }
        #endregion

        #region Helper Functions
        public void loadSkinSettings(string skinXMLFile)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(skinXMLFile);
                if (doc.DocumentElement != null)
                {
                    defines = LoadDefines(doc);
                    loadSettingsFromSkinSettings();
                }
            }
            catch
            {
                logger.WriteLog("Error loading skin settings from infoservice.xml: error in XML file", LogLevel.Error, InfoServiceModul.SkinSettings);
            }
        }

        private IDictionary LoadDefines(XmlDocument document)
        {
            Hashtable table = new Hashtable();
            try
            {
                foreach (XmlNode node in document.SelectNodes("/window/define"))
                {
                    if (node.InnerText.Contains("#InfoService.SkinSettings."))
                    {
                        logger.WriteLog("Found skin settings define in infoservice.xml: " + node.InnerText, LogLevel.Debug, InfoServiceModul.SkinSettings);

                        string[] tokens = node.InnerText.Split(new char[] { ':' }, 2);
                        if (tokens.Length < 2)
                        {
                            continue;
                        }
                        table[tokens[0]] = tokens[1];
                    }
                }
            }
            catch
            {
                logger.WriteLog("Error loading skin settings from infoservice.xml", LogLevel.Error, InfoServiceModul.SkinSettings);
            }
            return table;
        }

        private void loadSettingsFromSkinSettings()
        {
            if (defines != null)
            {
                using (settings = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml")))
                {
                    #region Feeds
                    string value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Feeds.Enabled"))
                        value = (string)defines["#InfoService.SkinSettings.Feeds.Enabled"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Feeds_Enabled = value.Trim();

                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Feeds.General.RefreshFeedEvery"))
                        value = (string)defines["#InfoService.SkinSettings.Feeds.General.RefreshFeedEvery"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Feeds_General_RefreshFeedEvery = value.Trim();

                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Feeds.Layout.ShowFeedItemPublishTime"))
                        value = (string)defines["#InfoService.SkinSettings.Feeds.Layout.ShowFeedItemPublishTime"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Feeds_Layout_ShowFeedItemPublishTime = value.Trim();

                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Feeds.Layout.MaxItemsPerFeedForAllFeeds"))
                        value = (string)defines["#InfoService.SkinSettings.Feeds.Layout.MaxItemsPerFeedForAllFeeds"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Feeds_Layout_MaxItemsPerFeedForAllFeeds = value.Trim();

                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Feeds.Layout.MaxItemsForFeedTicker"))
                        value = (string)defines["#InfoService.SkinSettings.Feeds.Layout.MaxItemsForFeedTicker"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Feeds_Layout_MaxItemsForFeedTicker = value.Trim();

                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Feeds.TickerLayout.TickerMask"))
                        value = (string)defines["#InfoService.SkinSettings.Feeds.TickerLayout.TickerMask"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Feeds_TickerLayout_TickerMask = value;

                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Feeds.TickerLayout.Separator"))
                        value = (string)defines["#InfoService.SkinSettings.Feeds.TickerLayout.Separator"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Feeds_TickerLayout_Separator = value;
                    #endregion

                    #region Weather
                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Weather.Enabled"))
                        value = (string)defines["#InfoService.SkinSettings.Weather.Enabled"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Weather_Enabled = value.Trim();

                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Weather.General.RefreshWeatherEvery"))
                        value = (string)defines["#InfoService.SkinSettings.Weather.General.RefreshWeatherEvery"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Weather_General_RefreshWeatherEvery = value.Trim();
                    #endregion

                    #region Twitter
                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Twitter.Enabled"))
                        value = (string)defines["#InfoService.SkinSettings.Twitter.Enabled"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Twitter_Enabled = value.Trim();

                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Twitter.General.RefreshTwitterEvery"))
                        value = (string)defines["#InfoService.SkinSettings.Twitter.General.RefreshTwitterEvery"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Twitter_General_RefreshTwitterEvery = value.Trim();

                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Twitter.Layout.MaxItemsForTwitterTicker"))
                        value = (string)defines["#InfoService.SkinSettings.Twitter.Layout.MaxItemsForTwitterTicker"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Twitter_Layout_MaxItemsForTwitterTicker = value.Trim();

                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Twitter.TickerLayout.TickerMask"))
                        value = (string)defines["#InfoService.SkinSettings.Twitter.TickerLayout.TickerMask"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Twitter_TickerLayout_TickerMask = value;

                    value = string.Empty;
                    if (defines.Contains("#InfoService.SkinSettings.Twitter.TickerLayout.Separator"))
                        value = (string)defines["#InfoService.SkinSettings.Twitter.TickerLayout.Separator"];
                    InfoServiceSkinSettings.InfoServiceSkinSettingsInstance.Twitter_TickerLayout_Separator = value;
                    #endregion
                }
                settings = null;
            }
        }
        #endregion
    }
}
